package cn.mxz.controller;

import cn.mxz.pojo.ResultData;
import cn.mxz.pojo.ShoppingCart;
import cn.mxz.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("addShoppingCart")
@CrossOrigin
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //根据登录的用户id查询所属购物车里物品
    @GetMapping("getShoppingCartByUserId")
    public ResultData<ShoppingCart> getShoppingCartByUserId(String userid){
        List<ShoppingCart> shoppingCarts = shoppingCartService.getShoppingCartByUserId(userid);
        return new ResultData(0,"查询成功",shoppingCarts);
    }

    //往购物车里添加一个订单
    @PostMapping("addOne")
    public ResultData<ShoppingCart> addShoppingCart( ShoppingCart shoppingCart){
        int i = shoppingCartService.addShoppingCart(shoppingCart);
        if(i>0){
            return new ResultData(0,"订单添加成功成功");
        }
        return new ResultData(210,"添加订单失败");
    }

    @DeleteMapping ("deleteOne")
    public ResultData<ShoppingCart> deleteShoppingCart( String  cart_id){
        int i = shoppingCartService.deleteShoppingCart(cart_id);
        if(i>0){
            return new ResultData(0,"订单删除成功");
        }
        return new ResultData(210,"删除订单失败");
    }

}
